//
//  MusicViewController.m
//  Project_B
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "MusicjianjieViewController.h"
#import "MusicViewController.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"
#import "MJRefresh.h"
#import "RCNetWorking.h"
#import "MusicTableViewCell.h"
#import "MusicModel.h"
#import "MusicPlayViewController.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface MusicViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSMutableArray *musicArr;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) UILabel * tagg;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) NSMutableArray *bigMusicArr;
@property (nonatomic, strong) UIButton * but;
@property (nonatomic, strong) NSMutableDictionary * musicdic;
@property (nonatomic, assign) NSInteger i;
@end

@implementation MusicViewController


- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.i = 1;
    
    [self initArraysandNSDictionary];
    
    [self data];
    [self creatableview];
    [self leftButton];
    //小菊花
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
    self.hud.animationType = MBProgressHUDAnimationFade;
        
}

- (void)initArraysandNSDictionary
{
    if (self.bigMusicArr == nil ) {
        self.bigMusicArr = [NSMutableArray array];
    }
    if (self.musicdic == nil ) {
        self.musicdic = [NSMutableDictionary dictionary];
    }
    if (self.musicdic == nil ) {
        self.musicdic = [NSMutableDictionary dictionary];
    }
}



#pragma mark - 左按钮
- (void)leftButton
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
}

- (void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - 刷新数据
- (void)setupRefresh
{
    self.table.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(prepareData1)];
}

#pragma mark - 准备加载数据
- (void)prepareData1
{
    self.i++;
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/%ld/20?device=iPhone", self.albumId, (long)self.i];
    
    [RCNetWorking GETDataWithAFN:str cookie:nil completion:^(id result) {
        NSArray *arr = result[@"tracks"][@"list"];
        self.musicArr = [MusicModel transformWithArray:arr];
        [self.bigMusicArr addObjectsFromArray:self.musicArr];
        [self.table reloadData];
        [self.table.mj_footer endRefreshing];
        
    } error:^(id error) {
        
    }];
}
#pragma mark - 准备数据
- (void)data
{
    NSString *str = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/1/20?device=iPhone", self.albumId];

    [RCNetWorking GETDataWithAFN:str cookie:nil completion:^(id result) {
        [self.hud hide:YES];
        self.musicdic = result[@"album"];
        NSArray *arr = result[@"tracks"][@"list"];
        self.tagg.text = self.musicdic[@"tags"];
        self.musicArr = [MusicModel transformWithArray:arr];
        self.bigMusicArr = self.musicArr;
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
    } error:^(id error) {
        
    }];
}


#pragma mark - headerView
- (void)headerView
{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -180.0, self.view.frame.size.width, 180)];
    [self.imageView setImage:[UIImage imageNamed:@"heise.jpg"]];
    //设置imageView高度改变时宽度也跟着改变
    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
    self.imageView.clipsToBounds = YES;
    [self.table addSubview:self.imageView];
    
    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, -120, 100, 100)];
    backgroundImage.backgroundColor = [UIColor redColor];
    [backgroundImage sd_setImageWithURL:[NSURL URLWithString:self.image]];
    [self.table addSubview:backgroundImage];
    
    UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(130, -120, self.view.frame.size.width - 140, 50)];
    name.numberOfLines = 0;
    name.backgroundColor = [UIColor clearColor];
    name.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    name.textColor = [UIColor whiteColor];
    name.text = self.name;
    [self.table addSubview:name];
    
    self.but = [UIButton buttonWithType:UIButtonTypeCustom];
    self.but.backgroundColor = [UIColor clearColor];
    [self.but addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    self.but.frame = CGRectMake(330, -70, 50, 50);
    [self.but setTitle:@">" forState:0];
//    self.but.backgroundColor = [UIColor orangeColor];
    //[self.but setTitle:self.intro forState:UIControlStateNormal];
    self.but.contentHorizontalAlignment =  UIControlContentHorizontalAlignmentLeft;
    self.but.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
    self.but.titleLabel.font = [UIFont systemFontOfSize: 15];
    self.but.titleLabel.textColor = [UIColor whiteColor];
    [self.table addSubview:self.but];
    
    self.tagg = [[UILabel alloc] initWithFrame:CGRectMake(130, -45, self.view.frame.size.width - 150, 20)];
    self.tagg.backgroundColor = [UIColor clearColor];
    self.tagg.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    self.tagg.textColor = [UIColor whiteColor];
    [self.table addSubview:self.tagg];
    
}

#pragma mark - 点击
- (void)click
{
    MusicjianjieViewController * main = [[MusicjianjieViewController alloc] init];
    main.titlea = self.musicdic[@"title"];
    main.nickname = self.musicdic[@"nickname"];
    main.tags = self.musicdic[@"tags"];
    main.intro = self.musicdic[@"intro"];
    [self.navigationController pushViewController:main animated:YES];
}

#pragma mark - 滑动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    CGFloat y = scrollView.contentOffset.y;//加上导航栏高度,第一次是-200
    if (y < -180) {
        CGRect frame = self.imageView.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
        self.imageView.frame = frame;
    }
}

#pragma mark - 创建tableView
- (void)creatableview
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 30) style:UITableViewStylePlain];
    self.table.dataSource = self;
    self.table.delegate = self;
    self.table.rowHeight = 70;
    self.table.contentInset = UIEdgeInsetsMake(180, 0, 0, 0);
    [self headerView];
    [self.view addSubview:self.table];
    [self.table registerClass:[MusicTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    [self setupRefresh];
}

#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bigMusicArr.count;
}

#pragma mark - 单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//无选中颜色
    cell.MusicModel = self.bigMusicArr[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#pragma mark - 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicPlayViewController *music = [[MusicPlayViewController alloc] init];
    music.MusiModel = self.bigMusicArr[indexPath.row];
    music.i = indexPath.row;
    music.musarr = self.bigMusicArr;
    [self.navigationController pushViewController:music animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
